package ywpt.web.action.people;

import java.util.ArrayList;
import java.util.List;

import org.ecside.table.limit.Limit;
import org.ecside.util.RequestUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.PeopleTrain;
import ywpt.service.people.TrainService;
import ywpt.web.common.CommonDictionary;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class TrainAction extends BaseAction implements
ModelDriven<PeopleTrain>, Preparable {

	/** 
	 * 培训情况Action
	 */
	private static final long serialVersionUID = -3564979621371584338L;
	private static int DEFAULT_PAGE_SIZE = 20;
	private Long id;
	private Long peopleId;
	
	private PeopleTrain trainInfo;
	private List<PeopleTrain> trainInfoList = new  ArrayList<PeopleTrain>();
	private TrainService trainService ;
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getPeopleId() {
		return peopleId;
	}

	public void setPeopleId(Long peopleId) {
		this.peopleId = peopleId;
	}

	public PeopleTrain getTrainInfo() {
		return trainInfo;
	}

	public void setTrainInfo(PeopleTrain trainInfo) {
		this.trainInfo = trainInfo;
	}

	public List<PeopleTrain> getTrainInfoList() {
		return trainInfoList;
	}

	public void setTrainInfoList(List<PeopleTrain> trainInfoList) {
		this.trainInfoList = trainInfoList;
	}

	public TrainService getTrainService() {
		return trainService;
	}

	public void setTrainService(TrainService trainService) {
		this.trainService = trainService;
	}
//**************************************************************
	@Override
	public PeopleTrain getModel() {
		return this.trainInfo;
	}

	@Override
	public void prepare() throws Exception {
		if(id!=null){
			this.trainInfo = trainService.getTrainInfoById(id);
		}else{
			this.trainInfo = new PeopleTrain();
		}
	}
//****************************************************************
	public String list() throws Exception{
		DetachedCriteria query = DetachedCriteria.forClass(PeopleTrain.class);
		query.createAlias("people", "people").add(Restrictions.eq("people.id", peopleId));
		int totalRows = RequestUtils.getTotalRowsFromRequest(getRequest());
		if (totalRows < 0) {
			totalRows = trainService.getTrainInfoCountByCriteria(query);
		}
		int[] rowStartEnd = RequestUtils.getRowStartEnd(getRequest(),
				totalRows, DEFAULT_PAGE_SIZE, 0);
		Limit limit = RequestUtils.getLimit(getRequest(), "ec", totalRows,
				DEFAULT_PAGE_SIZE);
		int pageSize = limit.getCurrentRowsDisplayed();
		query.addOrder(Order.asc("id"));
		query.setProjection(null);
		trainInfoList.addAll(trainService.getTrainInfos(query, pageSize,
				rowStartEnd[0]));
		getRequest().setAttribute("PAYMETHOD_MAP", CommonDictionary.PAYMETHOD);
		return SUCCESS;
	}

	public String input() 
	{
		this.setPeopleId(peopleId);
		trainInfo.setCost(0.0);
		return INPUT;
	}
	public String edit() 
	{
		trainInfo=this.trainService.getTrainInfoById(id);
		this.setPeopleId(trainInfo.getPeople().getId());
		return INPUT;
	}
	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() throws Exception {	

		try {
			if (id == null) 
			{
				trainService.saveTtrainInfo(trainInfo,peopleId);
				this.addActionMessage("培训情况新增成功!");
				return SUCCESS;
			} 
			else 
			{   				
				trainService.updateTrainInfo(trainInfo,peopleId);
				this.addActionMessage("培训情况更新成功!");
				return SUCCESS;					
			}
		} 
		catch (UniqueConstraintException e) 
		{	
			this.addActionError("保存错误:" + e.getMessage());
			return INPUT;
		}
	}	
	/**
	 * 删除
	 * 
	 * @return
	 */
	public String delete() throws Exception {		
		if (id != null) 
		{	
			trainService.removeTrainInfo(id);
			return SUCCESS;
		}
		this.addActionMessage("编号为空，请选择具体的记录！");
		return INPUT;
	}
}
